//
//  ViewController.m
//  UI_wangyi
//
//  Created by dllo on 16/6/22.
//  Copyright © 2016年 HeyGirls. All rights reserved.
//

#import "ViewController.h"
#import "textModel.h"
#import "textTableViewCell.h"
#import "imageTableViewCell.h"
#import "GetHeightTools.h"

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *modelArray;

@end

@implementation ViewController

static NSString *const textCellId = @"textCell";
static NSString *const imgCellId = @"imgCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    [self config];
    [self configBarButton];
    [self hanldeData];
    [self createTableView];
    // Do any additional setup after loading the view, typically from a nib.
    
}

-(void)config {
    
    self.modelArray = [[NSMutableArray alloc] init];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"网易";
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
}

-(void)hanldeData {
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"duanzi" ofType:@"txt"];
    
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    NSError *error = nil;
    
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    
    NSArray *textArray = [dictionary objectForKey:@"段子"];
    
    NSLog(@"textArray:%@", textArray);
    
    for (NSDictionary *dic in textArray) {
        textModel *model = [[textModel alloc] init];
        
        [model setValuesForKeysWithDictionary:dic];
        
        [self.modelArray addObject:model];
        
    }
    NSLog(@"%@", self.modelArray);
    
}

-(void)configBarButton {
    
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    UIImage *image = [UIImage imageNamed:@"搜索"];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)];
    
    UIImage *image2 = [UIImage imageNamed:@"直播"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image2 style:UIBarButtonItemStylePlain target:self action:@selector(leftAction:)];
    
    
}

-(void)createTableView {
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - 64)];
    [self.view addSubview:_tableView];
    [_tableView release];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.tableView.rowHeight = 550;
    
    [_tableView registerClass:[textTableViewCell class] forCellReuseIdentifier:textCellId];
    
    [_tableView registerClass:[imageTableViewCell class] forCellReuseIdentifier:imgCellId];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.modelArray.count;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    textModel *model = [self.modelArray objectAtIndex:indexPath.row];
    
    if (model.img == nil) {
        textTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:textCellId];
        
        textModel *model = [self.modelArray objectAtIndex:indexPath.row];
        
        cell.textModel = model;
        
        return cell;
    } else {
        
        imageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:imgCellId];
        
        textModel *model = [self.modelArray objectAtIndex:indexPath.row];
        
        cell.imageModel = model;
        
        return cell;
        
    }
    
    
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    textModel *model = [self.modelArray objectAtIndex:indexPath.row];
    
    if (model.img == nil) {
        return [GetHeightTools heightWith:model.digest] + 50 + 20;
    } else {
        
        return [GetHeightTools heightWith:model.digest] + [GetHeightTools twoHeightWith:model.img] + 50 + 20;
        
    }
    
}

-(void)leftAction:(UIButton *)button {
    
    
    
}

-(void)rightAction:(UIButton *)button {
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
